package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.ContractSetting;
import cn.stylefeng.guns.modular.yingbao.mapper.ContractSettingMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.ContractSettingParam;
import cn.stylefeng.guns.modular.yingbao.model.result.ContractSettingResult;
import cn.stylefeng.guns.modular.yingbao.service.ContractSettingService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 合约设置表 服务实现类
 * </p>
 *
 * @author jj
 * @since 2019-12-10
 */
@Service
public class ContractSettingServiceImpl extends ServiceImpl<ContractSettingMapper, ContractSetting> implements ContractSettingService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData add(ContractSettingParam param){
        ContractSetting entity = getEntity(param);
        Integer contractType = entity.getContractType();
        ContractSetting contractSetting = baseMapper.selectOne(new QueryWrapper<ContractSetting>().eq("contract_type", contractType));
        if (contractSetting!=null){
            return ResponseData.error("同等级别的合约设置只能有一个");
        }
        if (entity.getPrice()>entity.getPrice1()){
            return ResponseData.error("初始金额不能大于结束金额");
        }
        entity.setCreateTime(new Date());
        boolean save = this.save(entity);

        return save? ResponseData.success():ResponseData.error("错误！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(ContractSettingParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData update(ContractSettingParam param){
        ContractSetting oldEntity = getOldEntity(param);
        ContractSetting newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setPrice(newEntity.getPrice()*100);
        newEntity.setPrice1(newEntity.getPrice1() *100 );
        newEntity.setBuyingTime(newEntity.getBuyingTime()*60*60);
        newEntity.setBuyingVipTime(newEntity.getBuyingVipTime()*60*60);
        if (newEntity.getPrice()>newEntity.getPrice1()){
            return ResponseData.error("初始金额不能大于结束金额");
        }
        boolean b = this.updateById(newEntity);
        return b? ResponseData.success():ResponseData.error("错误！");
    }

    @Override
    public ContractSettingResult findBySpec(ContractSettingParam param){
        return null;
    }

    @Override
    public List<ContractSettingResult> findListBySpec(ContractSettingParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ContractSettingParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData applist() {

        List<ContractSetting> contractSettings = baseMapper.selectList(null);
        return contractSettings.size() > 0 ? ResponseData.success(200, "合约设置列表",contractSettings)
                : ResponseData.error("没有数据");
    }

    private Serializable getKey(ContractSettingParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ContractSetting getOldEntity(ContractSettingParam param) {
        return this.getById(getKey(param));
    }

    private ContractSetting getEntity(ContractSettingParam param) {
        ContractSetting entity = new ContractSetting();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
